<template>
<el-dialog
    :title="$t('plus.edit_level')"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="600px"
  >
    <el-form size="small" :model="form" ref="form">
      <el-form-item
        :label="$t('plus.level_name')"
        :label-width="formLabelWidth"
        prop="name"
        :rules="[{  required: true,  message: ' '}]"
      >
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <template v-if="form.isDefault == 0">
        <el-form-item
          :label="$t('plus.level_weight')"
          :label-width="formLabelWidth"
          prop="weight"
          :rules="[{  required: true,  message: $t('plus.input_level_weight')}]"
        >
          <el-input
            v-model="form.weight"
            type="number"
            :placeholder="$t('plus.input_level_weight')"
          ></el-input>
          <div class="gray9">{{$t('plus.higher_weight_higher_level')}}</div>
        </el-form-item>
        <el-form-item
          :label="$t('plus.level1_increment')"
          v-if="grade_level >= 1"
          :label-width="formLabelWidth"
          prop="firstPercent"
          :rules="[{  required: true,  message: $t('plus.input_level_weight')}]"
        >
          <el-input
            v-model="form.firstPercent"
            type="number"
            :placeholder="$t('plus.input_increment_ratio')"
          ></el-input>
          <div class="gray9"> {{$t('plus.increment_note')}} 

</div>
        </el-form-item>
        <el-form-item
          :label="$t('plus.level2_increment')"
          v-if="grade_level >= 2"
          :label-width="formLabelWidth"
          prop="secondPercent"
          :rules="[{  required: true,  message: $t('plus.input_level_weight')}]"
        >
          <el-input
            v-model="form.secondPercent"
            type="number"
            :placeholder="$t('plus.input_increment_ratio')"
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="$t('plus.level3_increment')"
          v-if="grade_level >= 3"
          :label-width="formLabelWidth"
          prop="thirdPercent"
          :rules="[{  required: true,  message: $t('plus.input_level_weight')}]"
        >
          <el-input
            v-model="form.thirdPercent"
            type="number"
            :placeholder="$t('plus.input_increment_ratio')"
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="$t('plus.auto_upgrade')"
          :label-width="formLabelWidth"
          prop="auto_upgrade"
        >
          <el-radio-group v-model="form.autoUpgrade">
            <el-radio :label="1">{{$t('plus.allow_auto_upgrade')}}</el-radio>
            <el-radio :label="0">{{$t('plus.disallow_auto_upgrade')}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('plus.upgrade_condition')" :label-width="formLabelWidth">
          <div>
            <el-radio-group v-model="form.conditionType">
              <el-radio label="and">{{$t('plus.condition_all')}}</el-radio>
              <el-radio label="or">{{$t('plus.condition_any')}}</el-radio>
            </el-radio-group>
          </div>
          <div class="d-s-c mt16">
            <el-checkbox v-model="form.openAgentMoney">{{$t('plus.total_commission')}}</el-checkbox>
            <el-input
              v-model="form.agentMoney"
              type="number"
              :disabled="form.openAgentMoney == 0"
              style="width: 160px; margin-left: 10px"
            ></el-input>
            <span class="ml10">{{$t('plus.unit_yuan')}}</span>
          </div>
          <div class="d-s-c mt16">
            <el-checkbox v-model="form.openAgentUser">{{$t('plus.direct_distributors')}}</el-checkbox>
            <el-input
              v-model="form.agentUser"
              type="number"
              :disabled="form.openAgentUser == 0"
              style="width: 160px; margin-left: 10px"
            ></el-input>
            <span class="ml10">{{$t('plus.people')}}</span>
          </div>
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">{{$t('plus.cancel')}}</el-button>
        <el-button type="primary" @click="editGrade" :disabled="submit_loading"
          >{{$t('plus.confirm')}}</el-button
        >
      </div>
    </template>
  </el-dialog>
</template>

<script>
import GradeApi from "@/api/plus/agent/grade.js";
export default {
  data() {
    return {
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      /*是否正在提交*/
      submit_loading: false,
      grade_level: 3
    };
  },
  props: ["open_edit", "form"],
  created() {
    this.form.openAgentMoney = this.form.openAgentMoney == 1 ? true : false;
    this.form.openAgentUser = this.form.openAgentUser == 1 ? true : false;
    this.dialogVisible = this.open_edit;
    this.getData();
  },
  methods: {
    getData() {
      let self = this;
      GradeApi.toEditgrade({}, true).then(res => {
        self.grade_level = parseInt(res.data.level);
      }).catch(error => {});
    },
    /*修改用户*/
    editGrade() {
      let self = this;
      let params = this.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.submit_loading = true;
          if (params.isDefault == 0) {
            params.openAgentMoney = params.openAgentMoney == true ? 1 : 0;
            params.openAgentUser = params.openAgentUser == true ? 1 : 0;
          } else {
            delete params.openAgentMoney;
            delete params.openAgentUser;
            delete params.agentMoney;
            delete params.agentUser;
          }
          GradeApi.editGrade(params, true).then(data => {
            self.submit_loading = false;
            ElMessage({
              message: $t("plus.level_update_success"),
              type: "success"
            });
            self.dialogFormVisible(true);
          }).catch(error => {
            self.submit_loading = false;
          });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("closeDialog", {
          type: "success",
          openDialog: false
        });
      } else {
        this.$emit("closeDialog", {
          type: "error",
          openDialog: false
        });
      }
    }
  }
};
</script>

